# -*- coding: utf-8 -*-
"""
@Time    : 2024/7/5 16:48 
@Author  : ZhangShenao 
@File    : book.py 
@Desc    : Book书籍类
"""


class Book:
    """图书类"""

    # 类常量
    WELCOME_INFO = 'Welcome! The content for this book is {content}.'

    def __init__(self, title: str, author: str, content: str):
        """
        构造函数
        :param title: 标题
        :param author: 作者
        :param content: 内容
        """
        self.title = title
        self.author = author
        self.__content = content  # __前缀的为私有属性

    # 成员函数
    def get_content_length(self) -> int:
        """获取内容长度"""
        return len(self.__content)

    # 成员函数
    def truncate_content(self, length: int) -> None:
        """
        截断内容
        :param length: 截断后的长度
        """
        self.__content = self.__content[:length]

    @classmethod  # 类函数,第一个参数默认为cls
    def create_empty_content_book(cls, title: str, author: str):
        """创建一个内容为空的Book"""
        return cls(title=title, author=author, content='')

    @staticmethod  # 静态函数
    def get_welcome_info(content) -> str:
        return Book.WELCOME_INFO.format(content=content)

    def __str__(self):
        """重写__str__方法,用于对象打印"""
        return f'Book(title={self.title}, author={self.author}, content_length={len(self.__content)})'


if __name__ == '__main__':
    # 创建book对象
    harry_potter_book = Book(title='Harry Potter',
                             author='J. K. Rowling',
                             content='... Forever Do not believe any thing is capable of thinking independently ...')

    # 调用book对象的方法
    print(f'before truncate, content length is: {harry_potter_book.get_content_length()}')  # 77
    harry_potter_book.truncate_content(10)
    print(f'after truncate, content length is: {harry_potter_book.get_content_length()}')  # 10

    # 在外部无法访问对象的私有属性
    # print(harry_potter_book.__content)  # AttributeError: 'Book' object has no attribute '__content')

    # 调用类函数
    empty_book = Book.create_empty_content_book(title='Empty Book', author='Unknown')
    print(f'empty_book: {empty_book}')
